/**     
* @Title: XStreamUtil.java   
* @Package me.crawler.util   
* @Description: TODO   
* @author xaoyaoyao
* @date 2019年1月12日 下午2:40:43
*/
package me.crawler.util;

import org.apache.commons.lang3.StringUtils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @ClassName: XStreamUtil
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2019年1月12日 下午2:40:43
 * 
 */
public class XStreamUtil {

	/**
	* @Title: xmlToBean   
	* @Description: 将xml字符串转化为java对象 (注解方式)
	* @param xml xml文件
	* @param clazz 转换的类对象
	* @return T
	 */
	public static <T> T xmlToBean(String xml, Class<T> clazz) {
		if (StringUtils.isNotBlank(xml) && null != clazz) {
			XStream xStream = new XStream(new DomDriver());
			xStream.processAnnotations(clazz);
			xStream.setClassLoader(clazz.getClassLoader());
			XStream.setupDefaultSecurity(xStream);
			xStream.allowTypesByWildcard(new String[]{clazz.getPackage().getName() + ".**"});
			Object object = xStream.fromXML(xml);
			T cast = clazz.cast(object);
			return cast;
		}
		return null;
	}
}
